/*
 * Copyright (C), 2013-2013, 上海汽车集团股份有限公司
 * Author:   陈迪
 * Date:     2014-01-03
 * Description: 从页面中移出，未改动内容
 * History: //修改记录
 * <author>		<time>			<desc>
 */

$(function(){
	$('#dop_alert_success .close').click(function(){
		$('#dop_alert_success, #dop_alert_Modal_bg').css('display', 'none');
		window.location.reload();
	});	
	$('#address').blur(function(){
		$(this).val($.trim($(this).val()));
	});
	ECar.validate.init({
		formid:'basicPersonalForm'
	});
	$('body').on('click','.btn[type="submit"]',function(e){
		$('input[validate="realname"]').val($.trim($('input[validate="realname"]').val()));
		$('input[validate="nickname"]').val($.trim($('input[validate="nickname"]').val()));
		$('span.m-err').each(function(){
			if($(this).css('display') != 'none'){
				return false;
			}
		});
		if(($('#province').val() === '0' && $('#city').val() === '0' && $('#town').val() === '0' && $('#address').val() === '') || ($('#province').val() !== '0' && $('#city').val() !== '0' && $('#town').val() !== '0' && $('#address').val() !== '')){
			$('#province_city_town').hide();
			if($(this).css('display') == 'none'){
				$('#addressdetail').hide();
			}
		}else if($('#province').val() !== '0' && $('#city').val() !== '0' && $('#town').val() !== '0' && $('#address').val() === ''){
			$('#addressdetail').show();
			$('#addressdetail span').text('请输入详细地址');
			return false;
		}else{
			$('#province_city_town').show();
			return false;
		}
		
		if($('#member_birthday').val() != '' && $('input[validate="cardnum"]').val() != ''){
			//过滤生日中的-
			var cardNum = $('#member_birthday').val();
	        var rs = "";
	        for (var i = 0; i < cardNum.length; i++) {
	        	if(cardNum.substr(i, 1) != '-')
	        		rs += cardNum.substr(i, 1);
	        }
	        if(($('input[validate="cardnum"]').val().length === 15) && ('19' + $('input[validate="cardnum"]').val().substr(6, 6)) != rs){
	        	$('#member_birthday').val('19' + $('input[validate="cardnum"]').val().substr(6, 2) + '-' + $('input[validate="cardnum"]').val().substr(8, 2) + '-' + $('input[validate="cardnum"]').val().substr(10, 2));
	        	return false;
	        }else if(($('input[validate="cardnum"]').val().length === 18) && ($('input[validate="cardnum"]').val().substr(6, 8)) != rs){
	        	$('#member_birthday').val($('input[validate="cardnum"]').val().substr(6, 4) + '-' + $('input[validate="cardnum"]').val().substr(10, 2) + '-' + $('input[validate="cardnum"]').val().substr(12, 2));
	        	return false;
	        }
		}
		
		e.preventDefault();
		if(ECar.validate.init.submit('basicPersonalForm')){
			$('#basicPersonalForm').ajaxSubmit({
			   success:function(data){
				   //2表示保存成功，且车辆认证成功
//				   if(data.errorCode==2){
//					   $('#dop_alert_Modal_bg, #dop_alert_success').css('display', 'block');
//				   }else if(data.errorCode==0){
				   if(data.errorCode==0 || data.errorCode==2){
						ECar.dialog.alert({message:'保存成功！',
							closeCallback:function(){
								window.location.reload();
							},callback:function(){
								window.location.reload();
							}
						});
					}
			   },
			   dataType:'json'
		   });  
		}
		return false;
	});

	$('#month,#year').change(function(){
		var y_val=$('#year').val();
		var flag=isLeapYear(y_val);
		var m_val=$('#month').val();
		var day=$('#day');
		if(m_val==4||m_val==6||m_val==9||m_val==11){
					
			var len=$('#day').find('option').length;
			if(len>30){
				day.find('option:last').remove();
			}else{
				for(var i=len;i<31;i++){
					day.append('<option value="">'+i+'</option>');
				}
			}
		}else if(m_val==2){
			var num=flag?29:28;	
			var len=$('#day').find('option').length;
			if(len>num){
				for(var i=num+1;i<len;i++){
					day.find('option:last').remove();	
				}
			}else{
				for(var i=len;i<num+1;i++){
					day.append('<option value="">'+i+'</option>');
				}
			}	

		}else{			
			var len=$('#day').find('option').length;
			if(len!=31){
				for(var i=len;i<32;i++){					
					day.append('<option value="">'+i+'</option>');
				}
			}			
		}
		$('#day').trigger('update');		
	});
	
	var isLeapYear=function(val){		
		if((val%4==0&&val%100!=0)||val%400==0){
			return true;
		}else{
			return false;
		}
	};
	
	//日期判断报错
	$('select#day, select#month, select#year').change(function(){
		if( $('#day').val()!='' && $('#month').val()!='' && $('#year').val()!='' ){
			$(this).parent().siblings('span.yearoldtxt').hide();
		}else{
			$(this).parent().siblings('span.yearoldtxt').show();
		}
	});
	
	//省市联动
	$('#province').change(function(){
		var val=$(this).val();
		$('#city').html('<option value="0">市</option>').trigger('update');
		$('#town').html('<option value="0">县</option>').trigger('update');
		getData($(this),val,'pro','',$('#city'));
		if($('#province').val() !== '0' || $('#city').val() !== '0' || $('#town').val() !== '0'){
			$('#province_city_town').hide();
		}
	});
	
	$('#city').change(function(){		
		var pro_val=$('#province').val();		
		var val=$(this).val();	
		$('[name="city_val"]').val(val);	
		getData($(this),pro_val,'',val,$('#town'));
		if($('#province').val() !== '0' || $('#city').val() !== '0' || $('#town').val() !== '0'){
			$('#province_city_town').hide();
		}
	});
	
	$('#town').change(function(){	
		$('[name="town_val"]').val($(this).val());
		if($('#province').val() !== '0' || $('#city').val() !== '0' || $('#town').val() !== '0'){
			$('#province_city_town').hide();
		}
	});
	
	$('#address').focus(function(){
		$('#addressdetail').hide();
	});
	$('#address').blur(function(){
		if($(this).val() != ''){
			if(!checkAddress($(this).val())){
				$('#addressdetail').show();
				$('#addressdetail span').text('地址格式错误');
			}
		}
	});

	var getData=function(obj,pro_val,type,city_val,obj){		
		var url= type!=''?'/member/cityInfo_' + pro_val +'.htm':'/member/districtInfo_'+pro_val+'_'+city_val+'.htm';		
		$.ajax({
			type:'POST',
			url:url,
			dataType: "json", 
			data:{'provinceId':pro_val,'cityId':city_val},
			success:function(data){			   		
				creatSelect(data,obj);
			}
		});
	};
	
	var creatSelect=function(data,obj){	
		 var len=data.length;		
		 var htm='<option value="0">'+(obj.attr('id')=='city'?'市':'县')+'</option>';		 
		 for(var i=0;i<len;i++){		 
		 	if(obj.attr('id')=='city'){
		 		var h='<option value="'+data[i]['cityId']+'">'+data[i]['cityName']+'</option>';
		 	}else{
		 		var h='<option value="'+data[i]['districtId']+'">'+data[i]['districtName']+'</option>';
		 	}		 	
		 	htm = htm + h;
		 }		 
		 obj.html(htm);
		 obj.trigger('update');
	};
	
	//当输入框有误，后面的绑定消失
	$('#validatePhone').blur(function(){
		var $this, $that, p;
		$this = $('#bindPhone');
		$that = $('#checkPhoneError');
		var alaFlag = checkPhone($(this).val());
		if(alaFlag == false){
			$this.hide();
			$that.show();
		}else{
			$this.show();
			$that.hide();
		}
	}).focus(function(){
		var $this, $that, p;
		$this = $('#bindPhone');
		$that = $('#checkPhoneError');
		$this.hide();
		$that.hide();
	});
	$('#validateMail').blur(function(){
		var $this, $that, p;
		$this = $('#bindMail');
		$that = $('#checkMailError');
		var alaFlag = checkMail($(this).val());
		if(alaFlag == false){
			$this.hide();
			$that.show();
		}else{
			$this.show();
			$that.hide();
		}
	}).focus(function(){
		var $this, $that, p;
		$this = $('#bindMail');
		$that = $('#checkMailError');
		$this.hide();
		$that.hide();
	});
	
	//性别问题
	$('[name="gender"]').click(function(){
		$(this).parent().find('span').hide();		
	});
	
	//出生日期问题
	$('#year,#month,#day').change(function(){
		if($(this).val()!=''){
			$(this).parent().find('span.m-user-tip').remove();
		}
	});
	
	//select统一下拉框效果,设置密保
    new ECar.uiform('select',{selectIgnoreFirstOption:false});
    
   $('.form-select').each(function(i){//设置select下拉层级问题
    	$(this).css({ 'zIndex': 100-i });
    });
});

//验证手机号码
function checkPhone(inputVal){
	if(!(/^1[3,4,5,7,8]\d{9}$/.test(inputVal)) && (inputVal != '')){
		return false;
	}else{
		return true;
	}
}

//验证邮箱
function checkMail(inputVal){
	if(!(/^[0-9a-zA-Z][_.0-9A-Za-z-]{0,31}@([0-9A-Za-z][0-9A-Za-z-]{0,30}\.){1,4}[A-Za-z]{2,4}$/.test(inputVal)) && (inputVal != '')){
		return false;
	}else{
		return true;
	}
}

//验证地址
function checkAddress(inputVal){
	if(!(/^[a-zA-Z\d\u4e00-\u9fa5]+$/).test(inputVal)){
		return false;
	}else{
		return true;
	}
}